module tool {
    export class WebSocketService extends egret.EventDispatcher {
        private webSocket: WebSocket;
        private hasConnection: boolean;
        public static SOCKET_CLOSE: string = "socket_close";
        public static SOCKET_DATA: string = "socket_data";
        public static SOCKET_CONNECT: string = "socket_connect"
        public constructor(webSocket: WebSocket) {
            super();
            this.webSocket = webSocket;
        }
        /**
         * 自动更新后端
         * **/
        public sendUpdate() {
            if (this.webSocket) {
                this.webSocket.send("@");
                if (this.webSocket.CONNECTING){
                    }
                   
            }
        }
        /**
         * 发送服务器
         * **/
        public sendMessage(sendObj) {
            // var  obj={"account_id":window.sessionStorage.getItem("account_id"),
            //           "operation":"CreateRoom",
            //           "data":{}
            //         }
            if (this.webSocket) {
                this.webSocket.send(JSON.stringify(sendObj));
            }
            

        }
        /**后端断开**/
        public connectionClosed() {
            this.hasConnection = false;
            this.dispatchEventWith(WebSocketService.SOCKET_CLOSE);
        }
        /**
         * 后端数据返回
         * **/
        public processMessage(data) {
            this.dispatchEventWith(WebSocketService.SOCKET_DATA, false, data);
        }
        /**帐号登录**/
        public authorize(token, verifier) {
            var sendObj = {
                type: 'authorize',
                token: token,
                verifier: verifier
            };
            this.webSocket.send(JSON.stringify(sendObj));
        }
    }
}